# -*- coding: utf-8 -*-
"""
@Created on: 2025/3/8 4:15
@Author: siebert
@Des: 事件监听
"""
from typing import Callable
from fastapi import FastAPI
from app.db.config import register_mysql
from app.modules.users.services import create_first_superuser


def startup(app: FastAPI) -> Callable:
    """
    FastApi 启动完成事件
    :param app: FastAPI
    :return: start_app
    """
    async def app_start() -> None:
        # APP启动完成后触发
        print('启动完毕')
        # 注册数据库
        await register_mysql(app)
        
        # 创建超级管理员
        try:
            await create_first_superuser()
        except Exception as e:
            print(f"创建超级管理员失败: {e}")

        # 初始化系统配置
        try:
            from app.modules.system.init_configs import init_system_configs
            await init_system_configs()
        except Exception as e:
            print(f"系统配置初始化失败: {e}")
        pass
    return app_start


def shutdown(app: FastAPI) -> Callable:
    """
    FastApi 停止事件
    :param app: FastApi
    :return: stop_app
    """
    def stop_app() -> None:
        # APP停止时触发
        print("停止")
    return stop_app
